package jayesh.gps;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DefaultDBHelper {
	public static final String DATABASE_NAME = "atmDB";
	public final static String ATMTABLE = "atmTable1";
	
    public static final String KEY_ATM_ID = "atmID";
    public static final String KEY_BANK_NAME = "bankName";
    public static final String KEY_ATM_ADD ="atmAdd";
    public static final String KEY_CITY ="city";
	public static final String KEY_LONGI = "longitude";
	public static final String KEY_LATI = "latitude";
	
	
	
	public DatabaseHelper mDbHelper;

	private static final String TAG = "AtmDBHelper";
	private SQLiteDatabase mDb;
    private static final int DATABASE_VERSION = 5;
    
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE1 =
        "CREATE TABLE IF NOT EXISTS " 
        + ATMTABLE 
        + " (atmID integer primary key autoincrement,bankName VARCHAR,atmAdd VARCHAR,city VARCHAR,longitude REAL,latitude REAL);";
	

    private final Context mCtx;
    
	private static class DatabaseHelper extends SQLiteOpenHelper {
	   
		DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        

    @Override
    public void onCreate(SQLiteDatabase db) {
    	db.execSQL(DATABASE_CREATE1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + ATMTABLE + ";");

        onCreate(db);
    	}
    }
	
	 public DefaultDBHelper(Context ctx) 
	    {
	        this.mCtx = ctx;
	    }

	 public DefaultDBHelper open() throws SQLException 
	    {
	        mDbHelper = new DatabaseHelper(mCtx);
	        mDb = mDbHelper.getWritableDatabase();
	        return this;
	    }
	    

		public void close() 
	    {
	        mDbHelper.close();
	    }

	// CRUD below
	    //delete
	    public boolean deleteEntryATM(int rowId)//long rowId) 
	    {
	    	if (mDb == null)
			{
				this.open();
			}
	    	//long id =rowId;
	        return mDb.delete(ATMTABLE, KEY_ATM_ID + "="+ rowId , null) > 0;
	    }
	 
	    
	    public Cursor fetchAllEntriesForTable() 
	    {
			if (mDb == null)
			{
				this.open();
			}

				return mDb.query("atmTable", new String[] { KEY_ATM_ID, KEY_BANK_NAME,KEY_ATM_ADD,KEY_CITY,KEY_LONGI,KEY_LATI}, null, null, null, null, null);
	    }	
	}
	/*

    public final static String MY_DATABASE_TABLE = "t_person";
     
    public DefaultDBHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            
            db.execSQL("CREATE TABLE IF NOT EXISTS " 
            + MY_DATABASE_TABLE 
            + " (LastName VARCHAR, FirstName VARCHAR," 
            + " Country VARCHAR, Age INT(3));");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w("My DB", "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS notes");
    onCreate(db);
    }
}	
*/	
